﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAO;
using BUS;
public partial class Admin_Pages_WRROLEAPPLYManager : BasePage
{
    public static bool Insert;
    BUS_WRROLEAPPLY busWRROLEAPPLY = new BUS_WRROLEAPPLY();
    BUS_WRROLE busWrrole = new BUS_WRROLE();
    BUS_WRUSERGROUP busWrusergroup = new BUS_WRUSERGROUP();
    BUS_WRUSER busWruser = new BUS_WRUSER();
    private void Show(string Mess)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + Mess + "')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            BindGrid();
            BindDrop();
        }
    }

    private void BindGrid()
    {
        grvWRROLEAPPLY.DataSource = busWRROLEAPPLY.Get_RoleApply_List();
        //grvWRROLEAPPLY.DataSource = busWRROLEAPPLY._GetList().Select(p=>new
        //{
        //    p.ID,
        //    RoleName = busWrrole._GetObject(Convert.ToInt32(p.IDRole)).RoleName,
        //    UserGroupName = busWrusergroup._GetObject(Convert.ToInt32(p.IDUserGroup)).UserGroupName,
        //    CreateDate = Common.Common.ConvertDateTime(p.CreateDate.ToString()) == "" ? "" : Convert.ToDateTime(p.CreateDate).ToShortDateString(),
        //    UpdateDate = Common.Common.ConvertDateTime(p.UpdateDate.ToString()) == "" ? "" : Convert.ToDateTime(p.UpdateDate).ToShortDateString(),
        //    NameCreate = p.IDCreateBy == (int?)null ? "" : busWruser._GetObject(Convert.ToInt32(p.IDCreateBy)).Lastname + " " + busWruser._GetObject(Convert.ToInt32(p.IDCreateBy)).Firstname,
        //    NameUpdate = p.IDUpdateby == (int?)null ? "" : busWruser._GetObject(Convert.ToInt32(p.IDUpdateby)).Lastname + " " + busWruser._GetObject(Convert.ToInt32(p.IDUpdateby)).Firstname,
        //    p.Description,
        //    p.Active

        //}).ToList();
        grvWRROLEAPPLY.DataBind();
    }

    public void BindDrop()
    {
        string lang = Request.Cookies["SelLang"].Value;
        if (lang == "vi")
        {
            dropIDRole.DataSource =
                busWrrole._GetList()
                    .Where(q => q.Active == "Y" && q.IDLang == 2)
                    .Select(p => new { p.ID, p.RoleName })
                    .ToList();
            dropIDRole.DataTextField = "RoleName";
            dropIDRole.DataValueField = "ID";
            dropIDRole.DataBind();
        }
        else
        {
            dropIDRole.DataSource = busWrrole._GetList().Where(q => q.Active == "Y" && q.IDLang == 1).Select(p => new { p.ID, p.RoleName }).ToList();
            dropIDRole.DataTextField = "RoleName";
            dropIDRole.DataValueField = "ID";
            dropIDRole.DataBind();
        }
        //dropIDRole.DataSource = busWrrole._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.RoleName }).ToList();
        //dropIDRole.DataTextField = "RoleName";
        //dropIDRole.DataValueField = "Id";
        //dropIDRole.DataBind();
        //dropIDUserGroup.DataSource = busWrusergroup._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.UserGroupName }).ToList();
        //dropIDUserGroup.DataTextField = "UserGroupName";
        //dropIDUserGroup.DataValueField = "Id";
        //dropIDUserGroup.DataBind();
        if (lang == "vi")
        {
            dropIDUserGroup.DataSource =
                busWrusergroup._GetList()
                    .Where(q => q.Active == "Y" && q.IDLang == 2)
                    .Select(p => new { p.ID, p.UserGroupName })
                    .ToList();
            dropIDUserGroup.DataTextField = "UserGroupName";
            dropIDUserGroup.DataValueField = "ID";
            dropIDUserGroup.DataBind();
        }
        else
        {
            dropIDUserGroup.DataSource = busWrusergroup._GetList().Where(q => q.Active == "Y" && q.IDLang == 1).Select(p => new { p.ID, p.UserGroupName }).ToList();
            dropIDUserGroup.DataTextField = "UserGroupName";
            dropIDUserGroup.DataValueField = "ID";
            dropIDUserGroup.DataBind();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = true;
        pnShow.Visible = false;
        Insert = true;
        txtDescription.Text = "";

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (grvWRROLEAPPLY.Items.Count > 0)
        {
            for (int i = 0; i < grvWRROLEAPPLY.Items.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvWRROLEAPPLY.Items[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    string Id = grvWRROLEAPPLY.Items[i].Cells[1].Text;
                    try
                    {
                        busWRROLEAPPLY._Delete(Convert.ToInt32(Id));
                    }
                    catch
                    {
                    }
                }
            }
        }
        BindGrid();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (dropIDUserGroup.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập IDUserGroup!");
            dropIDUserGroup.Focus();
            return;
        }
        if (dropIDRole.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập IDRole!");
            dropIDRole.Focus();
            return;
        }
        if (txtDescription.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Description!");
            txtDescription.Focus();
            return;
        }


        WRROLEAPPLY obj = new WRROLEAPPLY();
        obj.IDUserGroup = Convert.ToInt32(dropIDUserGroup.SelectedValue);
        obj.IDRole = Convert.ToInt32(dropIDRole.SelectedValue);
        obj.Description = txtDescription.Text;
        obj.Active = dropActive.SelectedValue;
        obj.UpdateDate = DateTime.Now;
        obj.IDUpdateby = Convert.ToInt32(Session["userid"].ToString());
        if (Insert)
        {
            obj.IDCreateBy = Convert.ToInt32(Session["userid"].ToString());
            obj.CreateDate = DateTime.Now;
            busWRROLEAPPLY._Add(obj);
        }
        else
        {
            obj.ID = Convert.ToInt32(txtID.Text);
            busWRROLEAPPLY._Update(obj);
        }
        BindGrid();
        pnUpdate.Visible = false;
        pnShow.Visible = true;
        Insert = false;

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = false;
        pnShow.Visible = true;

    }

    protected void grvWRROLEAPPLY_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            WRROLEAPPLY _WRROLEAPPLY = busWRROLEAPPLY._GetObject(Convert.ToInt32(Id));
            try
            {
                dropIDUserGroup.SelectedValue = _WRROLEAPPLY.IDUserGroup.ToString();
            }
            catch
            {
                Show("UserGroup is not active!");
            }
            try
            {
                dropIDRole.SelectedValue = _WRROLEAPPLY.IDRole.ToString();
            }
            catch
            {
                Show("Role is not active!");
            }
            txtID.Text = Id;
            txtDescription.Text = _WRROLEAPPLY.Description;
            dropActive.SelectedValue = _WRROLEAPPLY.Active.ToString();

            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else if (e.CommandName == "Delete")
        {
            busWRROLEAPPLY._Delete(Convert.ToInt32(Id));
            BindGrid();
        }

    }

    protected void grvWRROLEAPPLY_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grvWRROLEAPPLY.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
}
